<template>
    <div>
        <el-card>
            <!--搜索框-->
            <el-row :gutter="30">
                <el-col :span="10">
                    <el-input placeholder="请输入昵称" v-model="query.nickname" clearable @clear="getUserList">
                        <el-button slot="append" icon="el-icon-search" @click="getUserList"></el-button>
                    </el-input>
                </el-col>
                <el-col :span="4">
                    <el-button type="primary" @click="addDialogVisible = true">添加员工</el-button>
                </el-col>
            </el-row>
            <br />
            <!--员工列表-->
            <el-table :data="userList" border stripe>
                <el-table-column type="id" v-if="false">
                </el-table-column>
                <el-table-column prop="nickname" label="昵称" align="center" width="140px">
                </el-table-column>
                <el-table-column prop="username" label="账号" align="center">
                </el-table-column>
                <el-table-column prop="address" label="地址" align="center" :show-overflow-tooltip="true">
                </el-table-column>
                <el-table-column prop="email" label="邮箱" align="center">
                </el-table-column>
                <el-table-column prop="signature" label="个性签名" align="center" :show-overflow-tooltip="true">
                </el-table-column>
                <el-table-column prop="status" label="状态" align="center" width="140px">
                    <template slot-scope="scope">
                        <el-tooltip class="itemStatus" effect="dark" :content="scope.row.status == 0 ? '点击启用' : '点击停用' "
                            placement="right">
                            <el-switch v-model="scope.row.status" :active-value="1" :inactive-value="0"
                                @change="updateStatus(scope.row)"></el-switch>
                        </el-tooltip>
                    </template>
                </el-table-column>
                <el-table-column label="操作" align="center" width="182px">
                    <template slot-scope="scope">
                        <el-button size="mini" type="primary" icon="el-icon-edit" @click="handleEdit(scope.row)">编辑
                        </el-button>
                        <el-button size="mini" type="danger" icon="el-icon-delete" @click="handleDelete(scope.row.id)">
                            删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <!--分页组件-->
            <div>
                <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                    :current-page="query.pageNum" :page-sizes="[1, 5, 10, 50]" :page-size="query.pageSize"
                    layout="total, sizes, prev, pager, next, jumper" :total="total">
                </el-pagination>
            </div>
        </el-card>

        <!--添加员工-->
        <el-dialog title="添加员工" :visible.sync="addDialogVisible" @open="resetAddUserForm" width="50%">
            <el-form :model="addUserForm" :rules="userRules" ref="userFormRef" label-width="11%">
                <el-form-item label="员工昵称" prop="nickname">
                    <el-input v-model="addUserForm.nickname"></el-input>
                </el-form-item>
                <el-form-item label="账号" prop="username">
                    <el-input v-model="addUserForm.username" :readonly="editFlag"></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="password">
                    <el-input type="password" v-model="addUserForm.password" :readonly="editFlag"></el-input>
                </el-form-item>
                <el-form-item label="邮箱" prop="email">
                    <el-input v-model="addUserForm.email"></el-input>
                </el-form-item>
                <el-form-item label="头像" prop="portrait">
                    <el-input v-model="addUserForm.portrait"></el-input>
                </el-form-item>
                <el-form-item label="地址" prop="address">
                    <el-input v-model="addUserForm.address"></el-input>
                </el-form-item>
                <el-form-item label="个性签名" prop="signature">
                    <el-input v-model="addUserForm.signature"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="addDialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="addUser">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script>
    export default {
        created() {
            this.getUserList();
        },
        data() {
            return {
                query: {
                    nickname: null,
                    pageNum: 1,
                    pageSize: 10,
                },
                userList: [],
                total: 0,
                pageNum: 1,
                pageSize: 10,
                addDialogVisible: false,
                editFlag: false,
                addUserForm: {
                    id: null,
                    nickname: '',
                    username: '',
                    password: '',
                    email: '',
                    portrait: '',
                    address: '',
                    signature: '',
                },
                userRules: {
                    username: [
                        { required: true, message: '请输入账号', trigger: 'blur' },
                        { min: 5, max: 11, message: '长度为 5 ~ 11 个字符', trigger: 'blur' }
                    ],
                    password: [
                        { required: true, message: '请输入密码', trigger: 'blur' },
                        { min: 6, max: 24, message: '长度在 6 ~ 24 个字符', trigger: 'blur' }
                    ],
                },
            }
        },
        methods: {
            async getUserList() {
                if (this.query.nickname === '') {
                    this.query.nickname = null;
                }
                const { data: res } = await this.$http.get("/sys/user/employee/list", { params: this.query });
                this.userList = res.data.list;
                this.total = res.data.total;
                this.pageNum = res.data.pageNum;
                this.pageSize = res.data.pageSize;
            },
            addUser() {
                this.$refs.userFormRef.validate(async validate => {
                    if (!validate) {
                        return;
                    }
                    const { data: res } = await this.$http.post("/sys/user/employee/add", this.addUserForm);
                    this.addDialogVisible = false;
                    this.resetAddUserForm();
                    if (res.data) {
                        this.$message.success((this.addUserForm.id == null ? "添加" : "编辑") + "员工成功!");
                        this.getUserList();
                    }
                });
            },
            async updateStatus(row) {
                const { data: res } = await this.$http.post("/sys/user/employee/update", row);
                if (!res.data) {
                    this.getUserList();
                }
            },
            async handleEdit(row) {
                // const { data: res } = await this.$http.get("/sys/user/details/" + row.id);
            },
            handleDelete(id) {
                this.$elementMessageBox.confirm('删除后将无法恢复, 确认删除吗? ! ', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning',
                })
                    .then(async () => {
                        const { data: res } = await this.$http.delete("/sys/user/employee/delete/" + id);
                        if (res.data) {
                            this.$message.success("删除成功!");
                            this.getUserList();
                            return;
                        }
                        this.$message.error("删除失败!");
                    }).catch(() => {
                        this.$message.info("取消删除!");
                    });
            },
            handleSizeChange(val) {
                this.query.pageSize = val;
                this.getUserList();
            },
            handleCurrentChange(val) {
                this.query.pageNum = val;
                this.getUserList();
            },
            resetAddUserForm() {
                try {
                    this.$refs.userFormRef.resetFields();
                } catch (error) {

                }
            },
        }
    }
</script>
<style lang="less" scoped>

</style>